//- Copyright (c) 2016 Lucky Byte, Inc.
extends ../table

block form_items
  .field.thirteen.wide
    .ui.buttons.right.floated
      if page_perms.new
        button#new.ui.left.icon.labeled.green.button(type="button"
          onclick="window.location = '/roles/new'") 新 增
          i.icon.plus
      else
        button#new.ui.green.icon.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 新 增

block thead
  tr
    th.center.aligned 序号
    th.center.aligned 名称或代号
    th.center.aligned 权 限
    th.center.aligned 引用数
    th.center.aligned 备 注

block tbody
  each record, index in records
    tr(class="#{record.root ? 'positive' : ''}")
      td.collapsing
        if page_perms.edit || page_perms.disable
          i.icon.caret.right.link.detail.expand
          a(href="/roles/edit/#{record.serial}")= record.serial
        else
          i.icon.caret.right.link.detail.expand
          = record.serial
      td.center.aligned.collapsing= record.name
      if record.root
        td.left.aligned 可以执行任何操作
      else
        - var perms = ''
        - var count = 0
        each item in record.permission
          - perms += item.title + '、'
          - count += 1
        - perms = perms.substring(0, perms.length - 1)
        if perms.length < 50
          td.collapsing= perms
        else
          td.collapsing= perms.substring(0, 50)
            i.icon.ellipsis.horizontal.link.blue(
              data-content="#{perms}"
              data-variation="small very wide inverted")
            = '等 ' + count + '个菜单权限'

      td.center.aligned.collapsing
        a(href="/users?role=#{record.uuid}")= record.n_users
      td= record.notes

    tr.detail(style="display: none;")
      td.right.aligned.warning(colspan="1")
      td(colspan="4" style="padding-top:0px; padding-bottom:0px; padding-right:1px;")
        table.ui.celled.small.compact.table.very.basic.structured.nolayout
          tbody
            if record.root
              tr
                td.positive 可以执行任何操作
            else
              each item, index in record.permission
                if item.href && item.permission.access
                  - var label = '访问、'
                  if item.permission.new
                    - label += '增加、'
                  if item.permission.edit
                    - label += '修改、'
                  if item.permission.delete
                    - label += '删除、'
                  if item.permission.disabled
                    - label += '禁用/启用、'
                  if item.permission.check
                    - label += '复核、'
                  if item.permission.import
                    - label += '导入、'
                  if item.permission.export
                    - label += '导出、'
                  if item.permission.batch
                    - label += '批量操作、'

                  tr
                    td.collapsing.disabled= item.header || ''
                    td.collapsing= item.title
                    td.positive= label.substring(0, label.length - 1)
                    td.warning
                      code= item.href
